"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.EcsComponent = exports.RegObjProp = exports.RegComponentType = void 0;
var Utils_1 = require("./Utils");
/**
 * 注册组件的类型装饰器
 */
function RegComponentType() {
    return function (target) {
        var id = EcsComponent.__next_gen_id++;
        Object.defineProperty(target, '__compId', {
            value: id,
            writable: false,
            enumerable: false
        });
        if (!EcsComponent.__types[id]) {
            EcsComponent.__types[id] = target;
        }
    };
}
exports.RegComponentType = RegComponentType;
/**
 * 组件的属性标记为对象型，以便重置的时候进行深拷贝
 */
function RegObjProp() {
    return function (target, propertyKey) {
        if (!target.constructor.__objPropKeys) {
            target.constructor.__objPropKeys = [];
        }
        target.constructor.__objPropKeys.push(propertyKey);
    };
}
exports.RegObjProp = RegObjProp;
/**Ecs组件
 * !注意组件的每个属性必须有默认值，不然回收后不能重置该属性
 */
var EcsComponent = /** @class */ (function (_super) {
    __extends(EcsComponent, _super);
    function EcsComponent() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.__enable = true;
        _this.__ownerId = 0;
        return _this;
    }
    EcsComponent.getIdFromClz = function (compClz) {
        return compClz.__compId;
    };
    EcsComponent.getId = function (comp) {
        return comp.constructor.__compId;
    };
    EcsComponent.getComponentType = function (id) {
        return this.__types[id];
    };
    /**重置组件id生成器,在一个ECS组件定义文件中开始注册组件前调用 */
    EcsComponent.resetGenId = function () {
        this.__next_gen_id = 0;
    };
    EcsComponent.resetComp = function (comp, cst) {
        var instance = cst.instance();
        Object.assign(comp, instance);
        if (cst.__objPropKeys) {
            cst.__objPropKeys.forEach(function (key) {
                comp[key] = JSON.parse(JSON.stringify(instance[key]));
            });
        }
    };
    EcsComponent.__next_gen_id = 0;
    /**已经注册的组件类型 */
    EcsComponent.__types = {};
    return EcsComponent;
}(Utils_1.Singleton));
exports.EcsComponent = EcsComponent;
